package com.tjetc.simplefactory;

import java.util.Objects;

/**
 * 加载器
 */
public class ResourceLoader {
    /**
     * 根据路径返回资源
     * file:// classpath:// http:// https://
     *
     * @param path
     * @return
     */
    public Resource load(String path) {
        // 1.根据路径获取前缀
        String type = getPrefix(path);
//        Resource resource = null;
//        // 2.根据类型创建资源
//        if (Objects.equals("file", type)) {
//            // file的处理逻辑 省略.....
//            resource = new Resource(path);
//        } else if (Objects.equals("http", type)) {
//            // ..发起请求下载资源... 可能很复杂
//            resource = new Resource(path);
//        } else if (Objects.equals("classpath", type)) {
//            resource = new Resource(path);
//        } else {
//            resource = new Resource("default");
//        }
//        return resource;
        return ResourceFactory.create(type, path);
    }

    // 根据路径获取前缀
    private String getPrefix(String path) {
        if (Objects.isNull(path) || !path.contains(":") || Objects.equals("path", "")) {
            throw new ResourceLoadException();
        }
        String[] split = path.split(":");
        return split[0];
    }
}
